﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using ComicIsland.App.Resources;
using ComicIsland.Common;
using ComicIsland.Common.Entities.LocalDatabase;

/**
 * 创建时间：2015/1/12 11:06:32
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.App.CacheManageConverter
{
    public class ComicChapterProgressStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = ContentDownloadStatus.等待中.ToString();
            try
            {
                string ids = string.Empty;
                if (value != null)
                {
                    ids = value.ToString();
                }

                if (!string.IsNullOrWhiteSpace(ids))
                {
                    //var cpIds = new ParseComicBookPartIds();
                    //cpIds.ParseCBPId(comicAndPartId);
                    //if (cpIds.BookId > 0 && cpIds.ComicId > 0 && cpIds.PartId > 0)
                    //{
                    var cpIds = ids.Split('#');
                    if (cpIds != null && cpIds.Length == 3)
                    {
                        int comicId = 0, bookId = 0, partId = 0;
                        int.TryParse(cpIds[0], out comicId);
                        int.TryParse(cpIds[1], out bookId);
                        int.TryParse(cpIds[2], out partId);
                        //
                        if (comicId > 0 && bookId > 0 && partId > 0)
                        {
                            var items = App.dbContextHelper.GetAllComicChapterImageByComicIdAndChapterId(comicId, bookId, partId);
                            if (items != null && items.Count > 0)
                            {
                                var pauseTotal = items.Where(p => ContentDownloadStatus.已暂停.ToString().Equals(p.DownloadStatus)).Count();
                                if (pauseTotal > 0)
                                {
                                    result = ContentDownloadStatus.已暂停.ToString();
                                }
                                else
                                {
                                    int completedTotal = items.Where(p => ContentDownloadStatus.已完成.ToString().Equals(p.DownloadStatus)).Count();
                                    if (items.Count == completedTotal)
                                        result = ContentDownloadStatus.已完成.ToString();
                                    else
                                        result = ContentDownloadStatus.下载中.ToString();
                                }
                            }
                            else
                            {
                                var comicChatper = App.dbContextHelper.GetOneComicChapter(comicId, bookId, partId);
                                if (comicChatper != null)
                                {
                                    return comicChatper.DownloadStatus;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
